package com.wswy.wzcx.ui.main.community

import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.wswy.wzcx.R
import com.wswy.wzcx.module.base.Func1
import com.wswy.wzcx.module.base.loadLayout
import com.wswy.wzcx.statistics.StatTools
import com.wswy.wzcx.statistics.StatisticsId
import com.wswy.wzcx.ui.main.community.detail.TopicDetailActivity
import kotlinx.android.synthetic.main.topic_item_simple.view.*

class TopicListAdapter(private val holderLoginBlock : ((Func1)->Unit?)?,
                       private val headClickListener: View.OnClickListener?

                       ) : RecyclerView.Adapter<RecyclerView.ViewHolder>(),View.OnClickListener {


    companion object{
        private const val TAG = "TopicListAdapter"
    }

    val datas = arrayListOf<TopicVO>()

    override fun getItemViewType(position: Int): Int {
        return when(position){
            0 -> CommunityViewHolders.TYPE_ADD
            else -> datas[position-1].type
        }

    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {

        return when(viewType){

            CommunityViewHolders.TYPE_ADD -> CommunityViewHolders.SimpleTopicViewHolder(loadLayout(parent,R.layout.topic_item_add).apply {
                setOnClickListener(headClickListener)
            })
            CommunityViewHolders.TYPE_TOPIC_1 -> CommunityViewHolders.SimpleTopicViewHolder(loadLayout(parent,R.layout.topic_item_simple),holderLoginBlock,this)
            in CommunityViewHolders.TYPE_TOPIC_IMG_1..CommunityViewHolders.TYPE_TOPIC_IMG_9 -> CommunityViewHolders.SimpleImgViewHolder(viewType-CommunityViewHolders.BASE_IMG_TYPE,
                    loadLayout(parent,R.layout.topic_item_simple),holderLoginBlock,this)

            else -> CommunityViewHolders.SimpleTopicViewHolder(loadLayout(parent,R.layout.topic_item_add))
        }

    }


    override fun getItemCount(): Int = datas.size + 1

    override fun onBindViewHolder(viewHolder: RecyclerView.ViewHolder, position: Int) {

        if (position > 0){
            datas.getOrNull(position-1)?.run {
                viewHolder.itemView.layout_content.setTag(R.id.item_data,this)

                viewHolder.itemView.layout_content.setOnClickListener(this@TopicListAdapter)

                (viewHolder as? CommunityViewHolders.SimpleTopicViewHolder)?.bind(this)

            }
        }

    }

    override fun onClick(v: View?) {

        (v?.getTag(R.id.item_data) as? TopicVO)?.run {

            //列表-详情
            StatTools.sendClick(v.context,StatisticsId.bbs_list_detail)

            TopicDetailActivity.start(v.context,this.model.id)

        }?:kotlin.run {

            (v?.tag as? TopicVO)?.run {

                //列表-评论
                StatTools.sendClick(v.context,StatisticsId.bbs_list_comment)

                TopicDetailActivity.start(v.context,this.model.id,true)
            }


        }

    }

}